1 using System;
2 using
UnityEngine;
3 using
UnityStandardAssets.CrossPlatformInput;
4
5 namespace
UnityStandardAssets.Characters.FirstPerson
6 {
7     
[Serializable]
8     
public class MouseLook
9     {
10         
public float XSensitivity = 2f;
11         
public float YSensitivity = 2f;
12         
public bool clampVerticalRotation = true;
13         
public float MinimumX = -90F;
14         
public float MaximumX = 90F;
15         
public bool smooth;
16         
public float smoothTime = 5f;
17         
public bool lockCursor = true;
18
19
20         
private Quaternion m_CharacterTargetRot;
21         
private Quaternion m_CameraTargetRot;
22         
private bool m_cursorIsLocked = true;
23
24         
public void Init(Transform character, Transform camera)
25         {
26             m_CharacterTargetRot = character.localRotation;
27             m_CameraTargetRot = camera.localRotation;
28         }
29
30
31         
public void LookRotation(Transform character, Transform camera)
32         {
33             
float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
34             
float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;
35
36             m_CharacterTargetRot *= Quaternion.Euler (
0f, yRot, 0f);
37             m_CameraTargetRot *= Quaternion.Euler (-xRot,
0f, 0f);
38
39             
if(clampVerticalRotation)
40                 m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);
41
42             
if(smooth)
43             {
44                 character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
45                     smoothTime * Time.deltaTime);
46                 camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
47                     smoothTime * Time.deltaTime);
48             }
49             
else
50             {
51                 character.localRotation = m_CharacterTargetRot;
52                 camera.localRotation = m_CameraTargetRot;
53             }
54
55             UpdateCursorLock();
56         }
57
58         
public void SetCursorLock(bool value)
59         {
60             lockCursor =
value;
61             
if(!lockCursor)
62             {
//we force unlock the cursor if the user disable the cursor locking helper
63                 Cursor.lockState = CursorLockMode.None;
64                 Cursor.visible =
true;
65             }
66         }
67
68         
public void UpdateCursorLock()
69         {
70             
//if the user set "lockCursor" we check & properly lock the cursos
71             
if (lockCursor)
72                 InternalLockUpdate();
73         }
74
75         
private void InternalLockUpdate()
76         {
77             
if(Input.GetKeyUp(KeyCode.Escape))
78             {
79                 m_cursorIsLocked =
false;
80             }
81             
else if(Input.GetMouseButtonUp(0))
82             {
83                 m_cursorIsLocked =
true;
84             }
85
86             
if (m_cursorIsLocked)
87             {
88                 Cursor.lockState = CursorLockMode.Locked;
89                 Cursor.visible =
false;
90             }
91             
else if (!m_cursorIsLocked)
92             {
93                 Cursor.lockState = CursorLockMode.None;
94                 Cursor.visible =
true;
95             }
96         }
97
98         Quaternion ClampRotationAroundXAxis(Quaternion q)
99         {
100             q.x /= q.w;
101             q.y /= q.w;
102             q.z /= q.w;
103             q.w =
1.0f;
104
105             
float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
106
107             angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
108
109             q.x = Mathf.Tan (
0.5f * Mathf.Deg2Rad * angleX);
110
111             
return q;
112         }
113
114     }
115 }


Gõ tìm kiếm nhanh...